package UCM;

import Source.CreateAirport;
import Views.ViewCreateAirport;

/**
 *
 * @author Joshua King
 */
public class UCCreateAirport implements UCController {

    private ViewCreateAirport view;
    private CreateAirport model = new CreateAirport();

    public UCCreateAirport(ViewCreateAirport aThis) {
        view = aThis;
        view.loadTo(model);
    }

    /**
     * Precondition: The manager is logged in
     * 
    1. Manager enters details and clicks "Add Airport" button.
    2. System loads data to the model.
    3. System checks validity of the data.
    4. System commits data.
    5. System refreshes the add airport UI indicating success. 
     * 
     * Postcondition: The manager is on the add airport UI and a new flight has been created. 
     * 
     *
    3a) The details entered by the manager are inappropriate.
    1. System refreshes the UI and explains errors to the manager. 
    4a) System can't commit data.
    1. System updates the UI informing the manager that the service is currently unavailable. 
    4b) System receives notice that an error has occurred.
    1. System updates the UI informing the manager that there was an internal error. 
     */
    public void run() {
        // 2. System loads data to the model.
        view.loadData();
        //3. System checks validity of the data.
        if (model.checkValidity()) {
            //4. System commits data.
            if (model.commitData()) {
                //5. System refreshes the add airport UI indicating success. 
                view.refresh();
            } else {
//              4a) System can't commit data.
//              1. System updates the UI informing the manager that the service is currently unavailable.
//              4b) System receives notice that an error has occurred.
//              1. System updates the UI informing the manager that there was an internal error. 
                view.updateFailure();
            }
        } else {
            //3a) The details entered by the manager are inappropriate.
            //1. System refreshes the UI and explains errors to the manager.  
            view.updateFailure();
        }
    }
}
